import 'dart:ui';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import '../../common/index.dart';
import '../../pages/index.dart';

class MainPage extends GetView<MainController> {
  const MainPage({super.key});

  // 主视图
  Widget _buildView(int currentIndex) {
    // 根据当前索引显示不同的页面
    switch (currentIndex) {
      case 0:
        return const HomePage();
      case 1:
        return AiPage();
      case 2:
        return const MyPlantPage();
      case 3:
        return const MinePage();
      default:
        return const HomePage();
    }
  }

  // 构建底部导航项
  Widget _build_nav_item(String svgPath, int index, int currentIndex) {
    final bool is_active = currentIndex == index;

    return TweenAnimationBuilder<double>(
      duration: Duration(milliseconds: 300),
      tween: Tween<double>(begin: 0.8, end: 1.0),
      builder: (context, value, child) {
        return Transform.scale(scale: is_active ? 1.0 : value, child: child);
      },
      child: GestureDetector(
        onTap: () {
          // 添加振动反馈
          HapticFeedback.lightImpact();
          controller.switch_tab(index);
        },
        child: AnimatedContainer(
          duration: Duration(milliseconds: 300),
          curve: Curves.easeInOut,
          height: 36.h,
          padding: EdgeInsets.all(8.r),
          decoration: BoxDecoration(
            color: is_active ? null : Colors.transparent,
            gradient: is_active
                ? LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Color(0xFFFEED01), // 黄色
                      Color(0xFFB3A1FE), // 淡紫色
                    ],
                  )
                : null,
            borderRadius: BorderRadius.circular(24.r),
            boxShadow: is_active
                ? [
                    BoxShadow(
                      color: Color(0xFFFEED01).withOpacity(0.3),
                      blurRadius: 8,
                      spreadRadius: 2,
                    ),
                  ]
                : null,
          ),
          child: Center(
            child: AnimatedSwitcher(
              duration: Duration(milliseconds: 200),
              transitionBuilder: (child, animation) {
                return ScaleTransition(scale: animation, child: child);
              },
              child: SvgPicture.asset(
                svgPath,
                key: ValueKey<int>(is_active ? 1 : 0),
                width: 24.w,
                height: 24.h,
                colorFilter: ColorFilter.mode(
                  is_active ? Colors.black : Colors.grey,
                  BlendMode.srcIn,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  // 构建底部导航栏
  Widget _buildBottomNav(int currentIndex) {
    return AnimatedContainer(
      duration: Duration(milliseconds: 500),
      height: 90.h,
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 8.r),
      decoration: BoxDecoration(
        color: Color(0xFFCDFF5F),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(60.r),
          topRight: Radius.circular(60.r),
        ),
        border: Border(
          top: BorderSide(color: Color(0xFFFFEE00), width: 6.w),
          left: BorderSide(color: Color(0xFFFFEE00), width: 6.w),
          right: BorderSide(color: Color(0xFFFFEE00), width: 6.w),
        ),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 5.r,
            spreadRadius: 1.r,
          ),
        ],
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(54.r),
          topRight: Radius.circular(54.r),
        ),
        child: BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 15.1.r, sigmaY: 15.1.r),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              _build_nav_item(AssetsImages.home, 0, currentIndex),
              _build_nav_item(AssetsImages.ai, 1, currentIndex),
              _build_nav_item(AssetsImages.community, 2, currentIndex),
              _build_nav_item(AssetsImages.mine, 3, currentIndex),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainController>(
      init: MainController(),
      id: "main",
      builder: (controller) {
        return Scaffold(
          backgroundColor: Color(0xFFCDFF5F),
          extendBody: true,
          body: SafeArea(
            bottom: false,
            top: false,
            child: _buildView(controller.current_index),
          ),
          bottomNavigationBar: _buildBottomNav(controller.current_index),
        );
      },
    );
  }
}
